package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyStatisVoRedisService;
import cn.wolfcode.wolf2w.util.DateUtil;
import cn.wolfcode.wolf2w.util.RedisKey;
import cn.wolfcode.wolf2w.vo.StrategyStatisVo;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class StrategyStatisVoRedisServiceImpl implements IStrategyStatisVoRedisService {

    @Autowired
    private StringRedisTemplate template;
    @Autowired
    private IStrategyService strategyService;

    @Override
    public void viewnumIncr(String sid) {
        //获取对应攻略的vo对象
        StrategyStatisVo vo = this.getStrategyStatisVo(sid);

        //给统计数+1
        vo.setViewnum(vo.getViewnum() + 1 );

        //设置vo到redis中
        this.setStrategyStatisVo(vo);
    }


    @Override
    public StrategyStatisVo getStrategyStatisVo(String sid) {
        //拼接key
        String key = RedisKey.STRATEGY_STATIS_VO.join(sid);

        //先定义一个vo
        StrategyStatisVo vo = null;

        //判断key是否已经存在redis中
        if (!template.hasKey(key)) {
            //如果不存在的话,那么把对应的攻略先查出来
            Strategy strategy = strategyService.get(sid);
            //创建一个vo
            vo = new StrategyStatisVo();
            //把攻略的统计数给到vo对象
            BeanUtils.copyProperties(strategy, vo);
            //设置该vo的对应攻略的id
            vo.setStrategyId(sid);
        }else {
            //如果已存在那么拿出对应的vo
            String voJson = template.opsForValue().get(key);
            vo = JSON.parseObject(voJson,StrategyStatisVo.class);
        }
        return vo;
    }


    @Override
    public void setStrategyStatisVo(StrategyStatisVo vo) {
        //拼接key
        String key = RedisKey.STRATEGY_STATIS_VO.join(vo.getStrategyId());
        //把vo对象的Json格式字符串存到redis中
        template.opsForValue().set(key, JSON.toJSONString(vo));
    }

    @Override
    public void commentIncr(StrategyComment strategyComment) {
        //获取对应攻略的vo对象
        StrategyStatisVo vo = this.getStrategyStatisVo(strategyComment.getStrategyId());

        //给统计数+1
        vo.setReplynum(vo.getReplynum() + 1 );

        //设置vo到redis中
        this.setStrategyStatisVo(vo);

    }

    @Override
    public boolean addFavor(String sid, String uid) {
        //先拼接key
        String key = RedisKey.USER_STRATEGT_FAVOR_.join(uid);
        //查出对应攻略存在redis中的vo对象
        StrategyStatisVo vo = this.getStrategyStatisVo(sid);
        //定义一个List用于存储用户点赞过的攻略的id
        List<String> sids = new ArrayList<>();
        //判断redis是否有这个key的数据
        if (template.hasKey(key)){
            //如果已经存在的话,查出这个key对应的value
            sids = JSON.parseArray(template.opsForValue().get(key),String.class);
        }
        if (sids.contains(sid)){//如果攻略id已经在存在集合中表示要进行的取消操作
            sids.remove(sid);
            vo.setFavornum(vo.getFavornum()-1);
        }else {//如果不存在则进行的是点赞操作
            sids.add(sid);
            vo.setFavornum(vo.getFavornum()+1);
        }
        template.opsForValue().set(key,JSON.toJSONString(sids));
        this.setStrategyStatisVo(vo);
        return sids.contains(sid);
    }

    @Override
    public List<String> getFavor(String sid, String uid) {

        //拼接key
        String key = RedisKey.USER_STRATEGT_FAVOR_.join(uid);

        if (template.hasKey(key)){
            return JSON.parseArray(template.opsForValue().get(key),String.class);
        }

        return new ArrayList<>();
    }

    @Override
    public boolean strategyThumbup(String sid,String uid) {

        // 利用攻略id和当前登录用户的id来创建一个key来区分
        // 如果这个以这个key存在redis中表示该用户今天已经点过赞了

        String key = RedisKey.USER_STRATEGT_THUMBUP_.join(sid, uid);

        if (!template.hasKey(key)){

            //如果不存在的话
            //点赞数+1
            //并把记号添加到redis中缓存,并设值过期时间

            String jsonVo = template.opsForValue().get(RedisKey.STRATEGY_STATIS_VO.join(sid));
            StrategyStatisVo vo = JSON.parseObject(jsonVo, StrategyStatisVo.class);
            vo.setThumbsupnum(vo.getThumbsupnum()+1);
            template.opsForValue().set(RedisKey.STRATEGY_STATIS_VO.join(sid),JSON.toJSONString(vo));

            Date nowtime = new Date();//现在的时间
            Date endtime = DateUtil.getEndDate(nowtime);//今天的最后一秒

            Long time = DateUtil.getDateBetween(nowtime, endtime);//间隔时间

            template.opsForValue().set(key, "哈哈", time,TimeUnit.SECONDS);

            return true;//点赞成功
        }

        return false;//今天点过了
    }

}
